package xyz.jpcr.covid19app.Util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collection;

import xyz.jpcr.covid19app.Model.Coll;

public class IDBHelper {

    private static final String TABLE_NAME = "collection";
    private DBOpenHelper helper;
    private SQLiteDatabase db;

    public static class DBOpenHelper extends SQLiteOpenHelper {

        private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(id integer primary key autoincrement," +
                "name text not null," +
                "currentConfirmedCount integer not null," +
                "suspectedCount integer not null," +
                "curedCount integer not null," +
                "deadCount integer not null," +
                "confirmedCount integer not null," +
                "updateTime long not null);";

        public DBOpenHelper(@Nullable Context context, @Nullable String name) {
            super(context, name, null, 2);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop table if exists " + TABLE_NAME);
            onCreate(db);
        }
    }

    public IDBHelper(Context context) {
        helper = new DBOpenHelper(context, "COVID");
        db = helper.getWritableDatabase();
    }

    public boolean insert(String name, int currentConfirmedCount, int suspectedCount, int curedCount, int deadCount, int confirmedCount, long updateTime) {
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("currentConfirmedCount", currentConfirmedCount);
        values.put("suspectedCount", suspectedCount);
        values.put("curedCount", curedCount);
        values.put("deadCount", deadCount);
        values.put("confirmedCount", confirmedCount);
        values.put("updateTime", updateTime);
        return db.insert(TABLE_NAME, null, values) > 0;
    }

    public boolean query(String name) {
        Cursor cursor = db.query(TABLE_NAME, new String[]{"id", "name"}, "name=?", new String[]{name}, null, null, null);
        return cursor.getCount() > 0;
    }

    public boolean delete(String name) {
        return db.delete(TABLE_NAME, "name=?", new String[]{name}) > 0;
    }

    public Coll getByName(String name) {
        Cursor cursor = db.query(TABLE_NAME, new String[]{"id", "name", "currentConfirmedCount", "suspectedCount", "curedCount", "deadCount", "confirmedCount", "updateTime"}, "name=?", new String[]{name}, null, null, null);
        return new Coll(cursor.getString(cursor.getColumnIndex("name")),
                cursor.getInt(cursor.getColumnIndex("currentConfirmedCount")),
                cursor.getInt(cursor.getColumnIndex("suspectedCount")),
                cursor.getInt(cursor.getColumnIndex("curedCount")),
                cursor.getInt(cursor.getColumnIndex("deadCount")),
                cursor.getInt(cursor.getColumnIndex("confirmedCount")),
                cursor.getLong(cursor.getColumnIndex("updateTime")));
    }


    public ArrayList<Coll> getAll() {
        ArrayList<Coll> res = new ArrayList<>();
        Cursor cursor = db.query(TABLE_NAME, new String[]{"name", "currentConfirmedCount", "suspectedCount", "curedCount", "deadCount", "confirmedCount", "updateTime"}, null, null, null, null, null);
        while (cursor.moveToNext()) {
            res.add(new Coll(cursor.getString(cursor.getColumnIndex("name")),
                    cursor.getInt(cursor.getColumnIndex("currentConfirmedCount")),
                    cursor.getInt(cursor.getColumnIndex("suspectedCount")),
                    cursor.getInt(cursor.getColumnIndex("curedCount")),
                    cursor.getInt(cursor.getColumnIndex("deadCount")),
                    cursor.getInt(cursor.getColumnIndex("confirmedCount")),
                    cursor.getLong(cursor.getColumnIndex("updateTime"))
            ));
        }
        return res;
    }

}
